from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts,TitleOpts

import json

f = open("E:\excel学习文档-表格\黑马-可视化案例数据\地图-疫情.txt",'r',encoding  = "UTF-8")
data = f.read()
f.close()

# 判断是否为json文件
def is_json(text):
    try:
        json_text = json.loads(text)
        return True
    except:
        return False
print(is_json(data))



# data_dict = json.loads(data)
#
# # 提取省份的数据
# province_data_list = data_dict['areaTree'][0]['children']
# # print(province_data_list)
#
# # 组装每个省份和确诊人数为元组，并将各个省份的数据封装为列表
# data_list = []
# for province_data in province_data_list:
#     province_name = province_data['name']
#     province_confirm =province_data['total']['confirm']
#     print(province_data)
#
#     province_name += "省"
#     if province_name == "上海省":
#         province_name = "上海市"
#
#     if province_name == "北京省":
#         province_name = "北京市"
#
#     if province_name == "天津省":
#         province_name = "天津市"
#
#     if province_name == "重庆省":
#         province_name = "重庆市"
#
#     if province_name == "内蒙古省":
#         province_name = "内蒙古自治区"
#
#     if province_name == "广西省":
#         province_name = "广西壮族自治区"
#
#     if province_name == "新疆省":
#         province_name = "新疆维吾尔自治区"
#
#     if province_name == "西藏省":
#         province_name = "西藏自治区"
#
#     if province_name == "宁夏省":
#         province_name = "宁夏回族自治区"
#     # print(province_name)
#     # print(province_confirm)
#     data_list.append((province_name,province_confirm))  # 外面的括号表示append要添加的元素，里面的括号表示元组
# # print(data_list)
#
#
# # 创建地图对象
# map = Map()
#
# # 添加数据
# map.add("各省份确诊人数",data_list,"china")
# # 设置全局配置，定制分段的视觉映射
# map.set_global_opts(
#     title_opts = TitleOpts(title = "全国疫情地图",pos_left = "center",pos_bottom = "1%"),
#     visualmap_opts = VisualMapOpts(
#         is_show = True,
#         is_piecewise = True,
#         pieces = [
#             {"min":1,"max":99,"label":"1-99人","color":"FFCC33"},
#             {"min":100,"max":999,"label":"100-999人","color":"#66CC33"},
#             {"min":1000,"max":4999,"label":"1000-4999人","color":"#0099FF"},
#             {"min":5000,"max":9999,"label":"5000-9999人","color":"CC3333"},
#             {"min":10000,"label":">=10000人","color":"#663366"}
#         ]
#     )
# )
#
# # 绘图
# map.render("全国疫情地图.html")